package meijuli.com.join51.business.activity;

import android.Manifest;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.jhx.common.cache.SimpleCache;
import com.jhx.common.model.User;
import com.jhx.common.utils.Constants;
import com.jhx.common.utils.ToastUtil;
import com.jhx.common.utils.UserManager;
import com.jhx.common.widget.CommonDialog;

import java.util.List;

import meijuli.com.join51.business.R;
import meijuli.com.join51.business.utils.PermissionUtils;
import pub.devrel.easypermissions.EasyPermissions;

/**
 * 引导页
 *
 * @author wenqin 2017-04-15 10:30
 */
public class SplashActivity extends BaseActivity implements EasyPermissions.PermissionCallbacks {
    private ImageView mSplashView;
    /**
     * 是否授权
     */
    private boolean isAuthorization = false;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(meijuli.com.join51.business.R.layout.activity_splash);
        mSplashView = findViewById(R.id.splash_view);
        Glide.with(this).load(R.mipmap.img_splash).error(R.mipmap.img_default).crossFade(500).into(mSplashView);

        if (EasyPermissions.hasPermissions(this, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE)) {
            gotoActivity();
        } else {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                CommonDialog dialog = new CommonDialog(this);
                dialog.setMessage("为了保证聚惠侠商家版可以正常使用,请在设置-应用-聚惠侠商-权限,开启读、取sd卡权限");
                dialog.setConfirmBtn("设置");
                dialog.show();
                dialog.setOnClickListener(new CommonDialog.OnClickListener() {
                    @Override
                    public void onConfirmClick(View view) {
                        PermissionUtils.getInstance().toSetting(SplashActivity.this);
                        isAuthorization = true;
                    }

                    @Override
                    public void onCancelClick(View view) {
                        ToastUtil.getInstance().showToast("读、取sd卡权限被拒绝");
                        mSplashView.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                finish();
                            }
                        }, 1500);
                    }
                });
            }else {
                EasyPermissions.requestPermissions(this, "应用需要读、取sd卡权限，请授权",
                        100,
                        Manifest.permission.WRITE_EXTERNAL_STORAGE);
            }
        }
    }

    public void gotoActivity() {
        mSplashView.postDelayed(new Runnable() {
            @Override
            public void run() {
                toActivity();
            }
        }, 1000);
    }

    private void toActivity() {
        User user = UserManager.get().getUser();
        String token = SimpleCache.Companion.get(Constants.TOKEN_ID);
        if (user == null || TextUtils.isEmpty(token)) {
            Intent intent = new Intent(SplashActivity.this, LoginActivity.class);
            startActivity(intent);
        } else {
            Intent intent = new Intent(SplashActivity.this, MainActivity.class);
            startActivity(intent);
        }
        finish();
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        // 以下方法将把权限结果执行给EasyPermission
        EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (isAuthorization) {
            if (EasyPermissions.hasPermissions(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
                isAuthorization = false;
                gotoActivity();
            } else {
                finish();
            }
        }
    }

    @Override
    public void onPermissionsGranted(int requestCode, List<String> perms) {
        gotoActivity();
    }

    @Override
    public void onPermissionsDenied(int requestCode, List<String> perms) {
        CommonDialog dialog = new CommonDialog(this);
        dialog.setMessage("为了保证聚惠侠商家版可以正常使用,请在设置-应用-聚惠侠商-权限，开启读、取sd卡权限权限");
        dialog.setConfirmBtn("设置");
        dialog.show();
        dialog.setOnClickListener(new CommonDialog.OnClickListener() {
            @Override
            public void onConfirmClick(View view) {
                PermissionUtils.getInstance().toSetting(SplashActivity.this);
                isAuthorization = true;
            }

            @Override
            public void onCancelClick(View view) {
                ToastUtil.getInstance().showToast("读、取sd卡权限权限被拒绝");
                mSplashView.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        finish();
                    }
                }, 1500);
            }
        });
    }

}
